package biz.datalk.industrialland.common.util;

import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;

/**
 * 字段操作工具类
 *
 * @author tarofang@163.com
 * @date 2019-12-18
 */
public class FieldUtil {

    public static final String FIELD_UNDERLINE = "_";

    /**
     * 下划线转换为驼峰
     * @author tarofang@163.com
     * @date 2019-12-18
     * @param fieldName 下划线形式 字段名
     * @return 驼峰连接的字段名
     */
    public static String underline2Hump(String fieldName) {
        if (StringUtils.isBlank(fieldName)) {
            return fieldName;
        }
        if (!StringUtils.contains(fieldName, FIELD_UNDERLINE)) {
            return fieldName;
        }

        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, fieldName);
    }

    /**
     * 驼峰转换为下划线
     * @author tarofang@163.com
     * @date 2019-12-18
     * @param fieldName 驼峰形式 字段名
     * @return 下划线连接的字段名
     */
    public static String hump2Underline(String fieldName) {
        if (StringUtils.isBlank(fieldName)) {
            return fieldName;
        }
        if (StringUtils.contains(fieldName, FIELD_UNDERLINE)) {
            return fieldName;
        }
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
    }

    public static void main(String[] args) {
        try {

            String fieldName = "isDbsynch";

            String a = underline2Hump(fieldName);
            System.out.println(a);

            a = "tableName,tableComment,dynformVersion,isDbsynch";
            String b = hump2Underline(a);
            System.out.println(b);


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
